function Eb = WiSiL_Tx_averageBitEnergy( signal, ofdm, encoder, modulation, ...
                                         transmitter, frame )   
% WISIL_TX_AVERAGEBITENERGY Calculate bit energy
%
%   EB = WiSiL_Tx_averageBitEnergy( SIGNAL, OFDM, ENCODER, MODULATION,
%   TRANSMITTER, FRAME ) calculates the average bit energy EB of the
%   transmitted signal.
%   The average bit energy is important to be determined in a link-level
%   simulation, used, for example, in the generation of AWGN noise samples
%   with a given Eb/N0, and in graphics that use the EbN0 metric.
%   Here, it is important to observe that the calculation considers the
%   presence of the cyclic prefix.
%   SIGNAL is the time domain signal whose average bit energy is measured.
%   The following parameter structs, with respective fields are needed:
%   OFDM, containing the main OFDM parameters, in fields
%       .ifftsize,
%       .extSamples0, extSamples, the number of samples in the cyclic
%       prefix for the first symbol in a slot and for the remaining
%       symbols, respectively;
%   ENCODER.R, the coding rate;
%   MODULATION.M, the constellation size;
%   TRANSMITTER.txNum, the number of transmit antennas;
%   FRAME, containing information on the frame size and structure.

% History
% 27 Oct 2010, Ian Alves Ulian
%   function created and commented



Ref = encoder.R * ofdm.ifftSize / ( ofdm.ifftSize + ofdm.extSamples ) * ...
      ( ofdm.extSamples0 / ofdm.extSamples );
P = sum( sum( signal .* conj(signal) ) ) / transmitter.txNum;
E = P/(frame.N_DL_Symb*frame.slot);
Eb = E/((frame.numCarr-1) * (log2(modulation.M))*Ref);
